package com.sc.controller;

import com.github.pagehelper.PageInfo;
import com.sc.pojo.Post;
import com.sc.pojo.Postcomments;
import com.sc.service.PostService;
import com.sc.service.PostcommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/postComments")
public class PostcommentsController {
    @Autowired
    PostcommentsService pcs;
    @Autowired
    PostService ps;
    @RequestMapping("/add")
    public String add(Postcomments pc, Map m){
        pc.setTime(new Date());
        //添加一条评论
        pcs.add(pc);
        //找到该帖子
        Post p=ps.lookPost(pc.getPostid());
        m.put("p",p);
        //找到点赞该帖子的人
        List<Integer> goodList=ps.queryGoodId(pc.getPostid());
        m.put("goodList",goodList);
        return "/post/lookPost";
    }
    @RequestMapping("/show")
    public String show(@RequestParam(defaultValue = "1") Integer pageNum, Map m, HttpSession session){
        PageInfo<Postcomments> p;
        if(session.getAttribute("str")==null){
            //查所有数据
            p=pcs.show(pageNum,4);
        }else {
            //有条件的分页
            String str=(String) session.getAttribute("str");
            p=pcs.search(pageNum,4,str);
        }
        m.put("p",p);
        return "/post/commentMgr/list";
    }
    @RequestMapping("/search")
    public String search(@RequestParam(defaultValue = "1") Integer pageNum, Map m,String str,HttpSession session){
        if(!str.equals(session.getAttribute("str"))){
            session.setAttribute("str",str);
        }
        PageInfo<Postcomments> p=pcs.search(pageNum,4,str);
        m.put("p",p);
        return "/post/commentMgr/list";
    }
    @RequestMapping("/del")
    public String del(Integer id){
        pcs.del(id);
        return "redirect:/postComments/show";
    }
    @RequestMapping("/delAll")
    public String delAll(Integer[] ids){
        if (ids != null) {
            pcs.delAll(ids);
        }
        return "redirect:/postComments/show";
    }

}
